$(function(){
    getUserVerifyList();

    $('#side-menu ul').eq(0).addClass('in');

    $('#btn_search').on('click',function(){
        $("#pageNum").val(1);
        getUserVerifyList();
    });

    $(document).keydown(function(event){
        if(event.keyCode==13){
            $("#btn_search").click();
        }
    });

    //提交不通过描述
    $('#btn_add_auth_desc').on('click',function () {
        addAuthDesc();
        $('#descModal').modal('hide');
    });

});



/**
 *  功能描述：获取认证车主列表
 *  请求方式：GET
 *  请求地址：/api/verify/list
 *  函数名称：getUserVerifyList
 */

function getUserVerifyList(){
    $.ajax({
        url: whole_path+'/api/verify/list',
        type: 'GET',
        dataType: 'json',
        cache:false,
        data: $('#verify_form').serialize(),
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var list = json.result;
                var temp = "";
                if (list != null && list.length>0){
                    var operation = ''; //操作按钮
                    $.each(list, function (index, item) {
                        var status = item.checkStatus;
                        switch (status){
                            case 0:
                                status = '待认证';
                                operation = '<a href="#" class="btn-sm btn-info" data-placement="top" onclick="userAudit('+item.id+',2)" title="通过" ><i class="fa fa-thumbs-up"></i></a> &nbsp;'+' <a href="#" class="btn-sm btn-danger" title="不通过"   onclick="unPassMsg('+item.cuId+',1)"><i class="fa fa-thumbs-down"></i></a>'
                                break;
                            case 1:
                                status = '未通过';
                                operation = '已审核'
                                break;
                            case 2:
                                status = '已通过'
                                operation = '已审核'
                                break
                        }
                        var uploadTime = '';
                        if (item.uploadTime != null){
                            uploadTime = timestampFormat(item.uploadTime)
                        }
                        temp += '<tr class="gradeA">'
                            + '<td data-title="账号">' + item.userName + '</td>'
                            + '<td data-title="姓名">' + item.realName + '</td>'
                            + '<td data-title="身份证正面" class="content" ><img src="' + item.cardCorrect + '" style="width: 60px;height: 50px;" data-action="zoom" ></td>'
                            + '<td data-title="身份证反面" class="content" ><img src="' + item.cardOpposite + '" style="width: 60px;height: 50px;" data-action="zoom" ></td>'
                            + '<td data-title="手持身份证" class="content" ><img src="' + item.cardHand + '" style="width: 60px;height: 50px;" data-action="zoom" ></td>'
                            + '<td data-title="驾驶证照片" class="content" ><img src="' + item.drivingCard + '" style="width: 60px;height: 50px;" data-action="zoom" ></td>'
                            + '<td data-title="行驶证照片" class="content" ><img src="' + item.travelCard + '" style="width: 60px;height: 50px;" data-action="zoom" ></td>'
                            + '<td data-title="上传时间">' + uploadTime + '</td>'
                            + '<td data-title="审核状态">' + status + '</td>'
                            + '<td data-title="操作">' + operation + '</td>'
                            + '</tr>';
                    })
                    $('#verify_list tbody').html(temp);
                    page('#pagination', json.pagecount, json.pageindex, json.pagesize, getUserVerifyList, '#pageNum');

                }else{
                    $.toast("没有查到数据",3000);
                    $('#verify_list tbody').html('');
                    if($('#pagination').html().length > 0){
                        $('#pagination').jqPaginator('destroy');
                    }
                }
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}


/**
 *  功能描述：审核是否通过
 *  请求方式：POST
 *  请求地址：/api/verify/audit
 *  函数名称：userAudit
 */

function userAudit(id,status) {
    $.ajax({
        url:whole_path+ '/api/verify/audit',
        type: 'POST',
        dataType: 'json',
        data: {
            cuserId:id,
            checkStatue:status
        },
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:false,canCancel:true});
        },
        success:function(data){
            if(data.status == 0){
                $.toast('操作成功',3000);
                getUserVerifyList();
            }else{
                $.toast(data.msg,3000);
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}


function unPassMsg(id,status) {
    $('input[name=cuserId]').val('');
    $('input[name=checkStatue]').val('');
    $('#authVerifyMsg').val('');

    $('input[name=cuserId]').val(id);
    $('input[name=checkStatue]').val(status);
    $('#descModal').modal('show');
}

/**
 * 提交审核不通过
 */
function addAuthDesc() {
    $.ajax({
        url:whole_path+ '/api/verify/audit',
        type: 'POST',
        dataType: 'json',
        data: $('#add_auth_desc_form').serialize(),
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:false,canCancel:true});
        },
        success:function(data){
            if(data.status == 0){
                $.toast('操作成功',3000);
                getUserVerifyList();
            }else{
                $.toast(data.msg,3000);
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}